package JUC.CAS;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author CKC
 * @DateTime 2021/3/24 21:15
 */
public class ABADemo {
    /**
     * ABA问题的解决    AtomicStampedReference<V>
     */
    
    static AtomicReference<Integer> atomicReference = new AtomicReference<>(100);
    static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100, 1);
    
    
    public static void main(String[] args) {
//        AtomicStampedReference
        
        System.out.println("============一下是ABA问题的产生===============");
        new Thread(() -> {
            atomicReference.compareAndSet(100, 101);
            atomicReference.compareAndSet(101, 100);
        }, "t1").start();
        
        new Thread(() -> {
            try {//线程暂停一回儿，保证t1线程完成了一次ABA操作
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(atomicReference.compareAndSet(100, 2019) + "\t" + atomicReference.get());
        }, "t2").start();
        
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("============一下是ABA问题的解决===============");
        new Thread(() -> {
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName() + "\t  第一次版本号" + stamp);
            /** 暂停1秒钟t3线程*/
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            atomicStampedReference.compareAndSet(100, 101, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
            System.out.println(Thread.currentThread().getName() + "\t  第二次版本号" + atomicStampedReference.getStamp());
            
            atomicStampedReference.compareAndSet(101, 100, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
            System.out.println(Thread.currentThread().getName() + "\t  第三次版本号" + atomicStampedReference.getStamp());
        }, "t3").start();
        
        new Thread(() -> {
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName() + "\t  第一次版本号" + stamp);
            
            /** 暂停3秒钟 保证 t3 和 t4线程 获得的stamp是一样的、t3线程完成一次ABA操作*/
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean b = atomicStampedReference.compareAndSet(100, 1000, stamp, stamp + 1);
            int stamp1 = atomicStampedReference.getStamp();
            
            System.out.println(Thread.currentThread().getName() + "\t是否修改成功：" + b + "\t当前最新版本号：" + stamp1);
        }, "t4").start();
        
        
    }
}

